package com.chenfan.ccp.plug.business.parser;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Stack;

/**
 * @author xutong
 */
public class LogRecordContext {
    private static final InheritableThreadLocal<Stack<Map<String, Object>>> variableMapStack
            = new InheritableThreadLocal<>();


    public static Map<String, Object> popVariables() {
        if (isNotNull()) {
            return variableMapStack.get().pop();
        }
        return new HashMap<>();

    }

    private static boolean isNotNull() {
        return !Objects.isNull(variableMapStack.get());
    }

    public static Map<String, Object> getVariables() {
        if (isNotNull()) {
            return variableMapStack.get().peek();
        }
        return new HashMap<>();
    }

    /**
     * 添加自定义的参数
     * #name.field
     *
     * @param name   类名
     * @param object 对象
     */
    public static void putVariables(String name, Object object) {
        if (isNotNull()) {
            //peek取栈顶的数据，栈是如何实现了的？
            variableMapStack.get().peek().put(name, object);
        }
    }
    public static void addThreadLocal(Map<String, Object> map) {
        if (Objects.isNull(variableMapStack.get())) {
            variableMapStack.set(new Stack<>());
        }
        variableMapStack.get().push(map);
    }

    public static void clearThreadLocal(){
        variableMapStack.remove();
    }


}